package com.ruoyi.system.apiController;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ZcOrders;
import com.ruoyi.system.domain.ZcUsers;
import com.ruoyi.system.domain.ZcVehicles;
import com.ruoyi.system.service.IZcOrdersService;
import com.ruoyi.system.service.IZcUsersService;
import com.ruoyi.system.service.IZcVehiclesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出租车辆接口
 */
@RestController
@RequestMapping("/api/car/vehicles")
public class ZcVehiclesApiController {
    @Autowired
    private IZcVehiclesService zcVehiclesService;
    @Autowired
    private IZcOrdersService zcOrdersService;
    @Autowired
    private IZcUsersService zcUsersService;

    /**
     * 查询车辆列表
     */
    @GetMapping("/list")
    public AjaxResult list(ZcVehicles zcVehicles) {
//        return zcVehiclesService.selectZcVehiclesList(zcVehicles);
        List<ZcVehicles> list = zcVehiclesService.selectZcVehiclesList(zcVehicles);
        return AjaxResult.success(list);
    }

    /**
     * 租车
     */
    @PostMapping("/rent")
    public AjaxResult rent(@RequestParam(name = "userId", required = true) Long userId,
                           @RequestParam(name = "vehicleId", required = true) Long vehicleId,
                           @RequestParam(name = "startTime", required = true) String startTime,
                           @RequestParam(name = "endTIme", required = true) String endTime,
                           @RequestParam(name = "startLocation", required = true) String startLocation,
                           @RequestParam(name = "totalPrice", required = true) String totalPrice) {
        //查询用户是否存在
        ZcUsers zcUsers = zcUsersService.selectZcUsersByUserId(userId);
        if (ObjectUtil.isNull(zcUsers)){
            return AjaxResult.error("用户不存在");
        }
        ZcVehicles vehicles = zcVehiclesService.selectZcVehiclesByVehicleId(vehicleId);
        if (ObjectUtil.isNull(vehicles)){
            return AjaxResult.error("车辆不存在");
        }

        ZcOrders zcOrders = new ZcOrders();
        zcOrders.setUserId(userId);
        zcOrders.setVehicleId(vehicleId);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date startDate = dateFormat.parse(startTime);
            Date endDate = dateFormat.parse(endTime);
            zcOrders.setStartTime(startDate);
            zcOrders.setEndTime(endDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        zcOrders.setStartLocation(startLocation);
        zcOrders.setTotalPrice(new BigDecimal(totalPrice));
        zcOrders.setCreateTime(new Date());
        int i = zcOrdersService.insertZcOrders(zcOrders);
        Long orderId = zcOrders.getOrderId();
        Map<String, Object> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("msg","租车成功，请确认支付订单");
        if (i > 0){
            return AjaxResult.success(map);
        }else{
            return AjaxResult.error("租车失败");
        }
    }

    /**
     * 支付订单,取消订单
     */
    @PostMapping("/pay")
    public AjaxResult pay(@RequestParam(name = "orderId", required = true) Long orderId,
                          @RequestParam(name = "status", required = true) String status) {
        ZcOrders zcOrders = zcOrdersService.selectZcOrdersByOrderId(orderId);
        if (ObjectUtil.isNull(zcOrders)){
            return AjaxResult.error("订单不存在");
        }
        zcOrders.setStatus(status);
        int i = zcOrdersService.updateZcOrders(zcOrders);
        if (i >0 && "已支付".equals(status)){
            ZcVehicles zcVehicles = zcVehiclesService.selectZcVehiclesByVehicleId(zcOrders.getVehicleId());
            zcVehicles.setStatus("已出租");
            zcVehiclesService.updateZcVehicles(zcVehicles);
            return AjaxResult.success("支付成功");
        }else if (i >0 && "已取消".equals(status)){
            ZcVehicles zcVehicles = zcVehiclesService.selectZcVehiclesByVehicleId(zcOrders.getVehicleId());
            zcVehicles.setStatus("未出租");
            zcVehiclesService.updateZcVehicles(zcVehicles);
            return AjaxResult.success("取消成功");
        }else{
            return AjaxResult.error("失败");
        }
    }

    /**
     * 查询订单
     */
    @GetMapping("/order")
    public AjaxResult order(@RequestParam(name = "userId", required = true) Long userId) {
        ZcOrders zcOrders = new ZcOrders();
        zcOrders.setUserId(userId);
        List<ZcOrders> list = zcOrdersService.selectZcOrdersList(zcOrders);
        return AjaxResult.success(list);
    }
    /**
     * 查询订单详情
     */
    @GetMapping("/orderDetail")
    public AjaxResult orderDetail(@RequestParam(name = "orderId", required = true) Long orderId) {
        List<Map<String,Object>>  zcOrders = zcOrdersService.selectByOrderId(orderId);
        return AjaxResult.success(zcOrders);
    }


}
